﻿using System;
using UnityEditor;
using UnityEngine;

namespace EditorExtension
{
    public class PopupWindowExample : EditorWindow
    {
        [MenuItem("EditorExtensions/09_PopupWindow/Open")]
        static void Open()
        {
            GetWindow<PopupWindowExample>().Show();
        }

        private Rect mButtonRect;
        private void OnGUI()
        {
            if (Event.current.type == EventType.Repaint)
            {
                mButtonRect = GUILayoutUtility.GetLastRect();
            }
            
            if (GUILayout.Button("Open PopupWindow", GUILayout.Width(200)))
            {
                // 设置绘制的区域
                PopupWindow.Show(mButtonRect, new PopupWindowContentWindow());
            }
        }

        public class PopupWindowContentWindow : PopupWindowContent
        {
            private bool mToggle1 = true;
            private bool mToggle2 = true;
            private bool mToggle3 = true;
            
            public override void OnGUI(Rect rect)
            {
                base.OnGUI(rect);
                GUILayout.Label("Popup Options Example", EditorStyles.boldLabel);
                mToggle1 = GUILayout.Toggle(mToggle1, "Toggle 1");
                mToggle2 = GUILayout.Toggle(mToggle2, "Toggle 2");
                mToggle3 = GUILayout.Toggle(mToggle3, "Toggle 3");
            }

            public override void OnOpen()
            {
                base.OnOpen();
                Debug.Log("OnOpen");
            }

            public override void OnClose()
            {
                base.OnClose();
                Debug.Log("OnClose");
            }

            public override Vector2 GetWindowSize()
            {
                return new Vector2(200, 150);
            }
        }
    }
}